package theOrbalEra.common.network;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;

import theOrbalEra.common.TheOrbalEra;
import theOrbalEra.common.item.OrbalItemEleBag;
import theOrbalEra.common.item.OrbalItemQuartz;
import theOrbalEra.common.util.OrbalUtils;
import theOrbalEra.common.util.DefultValue;
import theOrbalEra.common.util.GetName;
import theOrbalEra.common.util.idList.PacketId;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.server.MinecraftServer;

import com.google.common.io.ByteArrayDataInput;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.network.PacketDispatcher;

public class PacketGetQuartz extends PacketBase{
	public static PacketGetQuartz instance=new PacketGetQuartz();
	@Override
	void readClient(int id, ByteArrayDataInput data, Object[] extradata) {
		
	}

	@Override
	void readServer(int id, ByteArrayDataInput data, Object[] extradata) {

		int loopID = data.readInt();
		int[] ele = {0,0,0,0,0,0,0};
		for(int i=0;i<7;i++)
			ele[i]=data.readInt();
		int[] need = {0,0,0,0,0,0,0};
		for(int i=0;i<7;i++)
			need[i]=data.readInt();
		EntityPlayer player=(EntityPlayer) extradata[0];
		getLoop(loopID,ele,need,player);
		
	}
	
	public void getLoop(int loopId, int[] ele, int[] need,EntityPlayer player) {
		if(player.capabilities.isCreativeMode){
			ItemStack loop=new ItemStack(TheOrbalEra.quartz,1,OrbalUtils.fromLoopIdGetEle(loopId));
			OrbalItemQuartz.setEle(loop, OrbalUtils.fromLoopIdGetEle(loopId));
			OrbalItemQuartz.setAble(loop, loopId);
			player.inventory.addItemStackToInventory(loop);
		}
		else{
			for(int i=0;i<7;i++){
				if(ele[i]-need[i]<0){
					player.addChatMessage(GetName.localize("player.insufficientBalance"));
					break;
					}
				}
			if(OrbalItemEleBag.hasbag(player)){
			int bagPos=OrbalItemEleBag.getBaginInv(player);
			int[] bag=OrbalItemEleBag.getEleCount(player.inventory.mainInventory[bagPos]);
			for(int i=0;i<7;i++){
				if(bag[i]-need[i]<=0){
					need[i]-=bag[i];
					bag[i]=0;
					}
				else{
					bag[i]-=need[i];
					need[i]=0;
					
				}
			}
			
			
			OrbalItemEleBag.setEleCount(player.inventory.mainInventory[bagPos], bag);
			}
			
			OrbalUtils.deStack(need[0], need[1], need[2], need[3], need[4], need[5], need[6],player.username);
			ItemStack loop=new ItemStack(TheOrbalEra.quartz,1,OrbalUtils.fromLoopIdGetEle(loopId));
			OrbalItemQuartz.setEle(loop, OrbalUtils.fromLoopIdGetEle(loopId));
			OrbalItemQuartz.setAble(loop, loopId);
			player.inventory.addItemStackToInventory(loop);
			}
	} 
	
	public static void getQuatz(int loopId,int[] ele,int[] need) {
		
		
		ByteArrayOutputStream bos = new ByteArrayOutputStream(110);
		DataOutputStream dos = new DataOutputStream(bos);
		try {
			dos.writeInt(PacketId.BANK_ACTION);
			dos.writeInt(loopId);
			for(int ver1:ele){
			dos.writeInt(ver1);
			}
			for(int ver2:need){
				dos.writeInt(ver2);
				}
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		Packet250CustomPayload pkt = new Packet250CustomPayload();
		pkt.channel = DefultValue.channel;
		pkt.data = bos.toByteArray();
		pkt.length = bos.size();
		pkt.isChunkDataPacket = false;
		PacketDispatcher.sendPacketToServer(pkt);
		
		
	}
	

	
}
